#ifndef HTTP_H
#define HTTP_H

#include <QObject>
#include <QNetworkRequest>          //请求
#include <QNetworkAccessManager>    //管理
#include <QNetworkReply>            //应答
#include <QDebug>
#include <QUrl>
#include <QList>

#define URL "http://apis.baidu.com/apistore/tranlateservice/translate"
#define MY_KEY "e3a55997d35c1164304c93f780bfc357"
// http://openapi.baidu.com/public/2.0/translate/dict/simple

class Http : public QObject
{
    Q_OBJECT
    Q_ENUMS(ApiTipe)    //将 enum 导出到 QML
    Q_PROPERTY(ApiTipe apiType READ getApiType WRITE setApiType)    //将 flagApiType 导出为 apiType
public:
    enum ApiTipe{   //API : 即时翻译, 百度翻译, 百度词典
        ApiNow, ApiBaidu, ApiWordBadu
    };
    enum WordApiType {  //词典API : 百度词典
        WordApiBaidu
    };
    explicit Http(QObject *parent = 0);
    ~Http();

    Q_INVOKABLE void setApiKey(const QString &apiKey); //导出到QML
    Q_INVOKABLE void setUrl(const QString &url);    //导出到QML

    ApiTipe getApiType() { return flagApiType; }
    void setApiType(ApiTipe type);  //设置要使用的API

signals:
    void sig_destMsg(QString str);    //发送翻译数据  //QML
    void sig_destWordMsg(int size, QList<QString> means, QString phMsg);    //发送词组数据(单词词义, 音标)  //QML

public slots:
    void setRequest(const QString &src, const QString from="auto", const QString to="auto");  //设置请求 也要在QML中使用
    void processData(); //处理数据
private:
    void parseNowApi(QByteArray replyStr);
    void parseBaiduApi(QByteArray replyStr);
    void preseBaiduWordApi(QByteArray replyStr);

    QNetworkRequest *request;
    QNetworkAccessManager *manager;
    QNetworkReply *reply;

    QString srcMsg;     //输入的字符串
    //翻译部分
    QString dstMsg;     //翻译完成的字符串
    //词典部分
    QList<QString> dstMeansList;    //返回的词组语义列表 (与词性对应)
    QString phMsg; //   音标

    QString myApiKey;
    QString url;
    ApiTipe flagApiType;   //选择要使用的翻译的API接口
};

#endif // HTTP_H
